package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.dao.DemoDept;
import com.ruoyi.demo.service.IDemoDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 部门Controller
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@RestController
@RequestMapping("/demo/dept")
public class DemoDeptController extends BaseController
{
    @Autowired
    private IDemoDeptService demoDeptService;

    /**
     * 查询部门列表
     */
    @HasPermi("demo:dept:list")
    @GetMapping("/list")
    public AjaxResult list(DemoDept demoDept)
    {
        List<DemoDept> list = demoDeptService.list(Wrappers.emptyWrapper());
        return AjaxResult.success(list);
    }

    /**
     * 导出部门列表
     */
    @HasPermi("demo:dept:export")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DemoDept demoDept)
    {
        List<DemoDept> list = demoDeptService.list(Wrappers.emptyWrapper());
        ExcelUtil<DemoDept> util = new ExcelUtil<DemoDept>(DemoDept.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @HasPermi("demo:dept:query")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") String deptId)
    {
        return AjaxResult.success(demoDeptService.getById(deptId));
    }

    /**
     * 新增部门
     */
    @HasPermi("demo:dept:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoDept demoDept)
    {
        return toAjax(demoDeptService.insert(demoDept));
    }

    /**
     * 修改部门
     */
    @HasPermi("demo:dept:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoDept demoDept)
    {
        return toAjax(demoDeptService.updateById(demoDept));
    }

    /**
     * 删除部门
     */
    @HasPermi("demo:dept:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable String[] deptIds)
    {
        return toAjax(demoDeptService.removeByIds(Arrays.asList(deptIds)));
    }
}
